//
//  LYUserAccountModel.swift
//  微博
//
//  Created by liuerying on 16/5/22.
//  Copyright © 2016年 liuerying. All rights reserved.
//

import UIKit


private let userInfoKey = "com.baidu.userInfoKey"

class LYUserAccountModel: NSObject {

    //单例对象  伪单例
    static let sharedAccountViewModel:LYUserAccountModel = LYUserAccountModel()

    var userAccount :LYUserAccount?
    
   private override init() {
        super.init()
    
        userAccount = loadUserAccount()
    
    
    }
    
    var userLogin: Bool {
        //如果模型里的使用时间 与现在时间相比   是降序的话  就是已经登录
        if userAccount?.expires_date?.compare(NSDate()) == .OrderedDescending {
            return true
        }
        
        //如果过期  需要将 userAccount 设置为 nil
        userAccount = nil
        return false
    }
    
    //只读属性
    var headURL:NSURL?{
    
        get {
        
            return NSURL(string: userAccount?.avatar_large ?? "")
        }
    }
}




extension LYUserAccountModel{

    //使用code 加载用户授权的token(令牌)
    func loadAccessToken(code: String,finished: (isSuccess: Bool)->() ) {
        
        LYNetworkTools.sharedTools.loadAccessToken(code) { (responseObject, error) in
            print(responseObject,error)
            
            if error != nil{
                finished(isSuccess: false)
             //请求失败
                return
  }
                            //请求成功 后加载用户信息
           
            self.loadUserInfo(responseObject, finished: finished)
                
             }

        }
    
    func loadUserInfo(response:AnyObject? ,finished:(isSuccess: Bool)->() ){
        //请求成功后的字典数据
        guard let result = response as? [String : AnyObject] else {
            
            finished(isSuccess: false)
            return
        }
        
        LYNetworkTools.sharedTools.loadUserInfo(result) { (responseObject, error) in
            if  error != nil{
                
                finished(isSuccess: false)
                return
            }
            
            
            guard var dict = responseObject as? [String : AnyObject] else {
                //如果不是字典类型
                return
            }
            
            //合并两个字典,统一存储用户的数据到本地
            for (key, value) in result {
                
                dict[key] = value
                
                
            }
            self.saveUserAccount(dict)
            
            
            finished(isSuccess: true)
        }
    }
    
    }


//存储数据
extension LYUserAccountModel {

    func saveUserAccount(dict: [String : AnyObject]){
    
        //讲字典转模型对象
        let user = LYUserAccount(dict: dict)
        
        self.userAccount = user
        
        //讲对象转为模型  --为了把其他没用的去掉,留下有用的字典
       let keys = ["access_token","expires_in","uid","name","avatar_large","expires_date"]
        
        
        let userInfo = user.dictionaryWithValuesForKeys(keys)
        
        print(userInfo)
        
        //保存字典数组
        let userDefaultes = NSUserDefaults.standardUserDefaults()
        
        //不能向偏好设置中保存nil
        userDefaultes.setObject(userInfo, forKey: userInfoKey)
        //同步
        userDefaultes.synchronize()
        
//        print(<#T##items: Any...##Any#>)
        
    }
    
    
    func loadUserAccount()->LYUserAccount?{
    
        guard let dict = NSUserDefaults.standardUserDefaults().objectForKey(userInfoKey)as? [String : AnyObject] else{
        
            return nil
        }
        
        let user = LYUserAccount(dict: dict)
        return user
    }
}
